package com.yuyou.fn.settlement.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.rocketmq.Producer;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.settlement.vo.PayRecordQueryVo;
import com.yuyou.fn.settlement.vo.PayWay;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yuyou.fn.settlement.mapper.NewPayRecordMapper;
import com.yuyou.fn.settlement.entity.NewPayRecord;
import com.yuyou.fn.settlement.service.INewPayRecordService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 *
 * NewPayRecord 表数据服务层接口实现类
 *
 */
@Service
public class NewPayRecordServiceImpl extends SuperServiceImpl<NewPayRecordMapper, NewPayRecord,Long> implements INewPayRecordService {

    private final Logger log = LoggerFactory.getLogger(NewPayRecordServiceImpl.class);

    @Resource
    private Producer producer ;

    @Override
    public void savePayRecord(Long busOrderId, int busType, boolean firstPay, BigDecimal payAmount, Long chargeCampusId, List<PayWay> payWays, String extra, Principals.Principal principal) {

        NewPayRecord newPayRecord = new NewPayRecord();
        newPayRecord.setBusOrderId(busOrderId);
        newPayRecord.setBusType(busType);
        newPayRecord.setAmount(payAmount);
        newPayRecord.setCampusId(chargeCampusId);
        newPayRecord.setFirstPay(firstPay?1:0);
        newPayRecord.setOperator(principal.name);
        newPayRecord.setExtra(extra);
        newPayRecord.setPayWays(JSONObject.toJSONString(payWays));
        newPayRecord.setCreateTime(TimeUtils.now());
        newPayRecord.setUpdateTime(TimeUtils.now());
        insertSelective(newPayRecord);

        try{
            producer.send("pay_record_notify",null , JSONObject.toJSONBytes(newPayRecord));
        }catch (Exception e){
            log.error("Send mq fail,topic = pay_record_notify " , e );
        }
    }

    @Override
    public List<NewPayRecord> findByBusOrderId(Long busOrderId) {
        Example<NewPayRecord> example = NewPayRecord.createExample();
        example.createCriteria().andEqualTo("busOrderId",busOrderId);
        return findListByExample(example);
    }

    @Override
    public List<NewPayRecord> findByBusOrderIds(List<Long> busOrderIds) {
        Example<NewPayRecord> example = NewPayRecord.createExample();
        example.createCriteria().andIn("busOrderId",busOrderIds);
        return findListByExample(example);
    }

    @Override
    public Page<NewPayRecord> findPayRecordByQuery(PayRecordQueryVo payRecordQueryVo, int pageNo, int pageSize) {
        if(payRecordQueryVo.getEndDate() != null ){
            payRecordQueryVo.setEndDate(DateUtils.addDays(payRecordQueryVo.getEndDate(),1));
        }

        Example<NewPayRecord> example = NewPayRecord.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,payRecordQueryVo);
        return findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public NewPayRecord findFirstPayByBusOrderId(long busOrderId) {

        Example<NewPayRecord> example = NewPayRecord.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("busOrderId",busOrderId).andEqualTo("firstPay",1);
        return this.findOneByExample(example);
    }
}